package treemap;


/*TreeMap的底层原理是红黑树
TreeMap树的节点是Entry 对象，Entry是TreeMap的一个内部类。

ConcurrentHashMap替代HashMap
ConcurrentSkipListMap替代TreeMap
ConcurrentSkipListSet替代TreeSet
CopyOnWriteArrayList替代ArrayList
CopyOnWriteArraySet替代HashSet*/

import java.util.*;


//https://blog.csdn.net/scgyus/article/details/79105211  map四种遍历方法
public class Demo01 {

    public static void main(String[] args) {

        // 比如 String -key String -val
        // 希望 key 按照 长度 的从大到小

      TreeMap<String,String> map= new TreeMap<String,String>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {

                //自定义  按照键的大小排序
                return Integer.valueOf(o1)-Integer.valueOf(o2);
            }
        });

        map.put("111", "hello01");
        map.put("22222", "hello02");
        map.put("33", "hello03");
        map.put("3666", "hello05");
        map.put("4", "hello02");
         map.put("5", null);
        System.out.println(map);


        //map遍历   entry
        Set<Map.Entry<String, String>> set = map.entrySet();

        for (Map.Entry<String, String> entry : set) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }


        //第二种获取方式
        Set<String> strings = map.keySet();
        for (String string : strings) {
            System.out.println(string);
        }
        Collection<String> values = map.values();
        for (String value : values) {
            System.out.println(value);
        }


        //map遍历   entry   迭代器遍历
        Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
        while (entries.hasNext()){
            Map.Entry<String, String> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }



    }
}
